import logging

DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL


def get_logger(path, to_file=True, stream=False, leve=INFO, formatter=None):
    logger = logging.getLogger(path)
    logger.handlers.clear()
    logger.setLevel(leve)

    if formatter is None:
        formatter = logging.Formatter('%(asctime)s-%(levelname)s : %(message)s', '%Y-%m-%d %H:%M:%S')

    if stream:
        # 创建一个StreamHandler,用于输出到控制台
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(leve)
        stream_handler.setFormatter(formatter)
        logger.addHandler(stream_handler)

    if to_file:
        # 创建一个FileHandler，用于写到本地 使用RotatingFileHandler类，滚动备份日志
        fh = logging.handlers.RotatingFileHandler(filename=path,
                                                  mode='a',
                                                  maxBytes=1024 * 1024 * 1000,
                                                  backupCount=5,
                                                  encoding='utf-8')
        fh.setLevel(leve)
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    return logger